package com.pcjavanet.generate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ChangeBeanUtil {
	private String filepath;
	private String modelPackagePath;
	private boolean isSave = true ; 
	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}

	public void changeBean(){
		String content = getNewContent();
		if ( isSave )
		FileUtil.contentToSave(filepath, content);
	}
	
	private  List<String> getRefNames(){
		String row;
		List<String> refNames = new ArrayList<String>();
		try {
			FileReader read = new FileReader(filepath);
			BufferedReader br = new BufferedReader(read);
			while ((row = br.readLine()) != null) {
				if (row.indexOf("ref") != -1 && row.indexOf("Integer") != -1
						&& row.indexOf("private") != -1) {
					int refBegin = row.indexOf("ref");
					int refEnd = row.indexOf(";");
					String tmp = row.substring(refBegin, refEnd);
					refNames.add(tmp);
				}
			}
			read.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return refNames ; 
	}
	
	private List<String> getBeanNames(List<String> refFieldNames){
		List<String> rtvs = new ArrayList<String>();
		for( int i = 0 ; i < refFieldNames.size() ; i++ ) {
			String fieldName = refFieldNames.get(i);
			int beanIndex = 0;
			int beanEnd = 0;
			for (int j = 0; j < fieldName.length(); j++) {
				char c = fieldName.charAt(j);
				// A =65 Z=90 a=97 z=122
				if (c >= 65 && c <= 90) {
					if (beanIndex == 0) {
						beanIndex = j;
						continue;
					}
					if (beanIndex != 0 && beanEnd == 0) {
						beanEnd = j;
						break;
					}
				}
			}
			if (beanIndex == 0 || (beanIndex != 0 && beanEnd == 0)) {
				System.err.print("taken error :" + fieldName);
			} else {
				String beanName = fieldName.substring(beanIndex, beanEnd);
				rtvs.add( beanName );
			}
			
		}
		return rtvs;
	}
	
	private String getNewContent() {
		StringBuffer content = new StringBuffer();
		try {
			if ( (new File ( filepath)).exists() == false ){
				filepath = filepath.replace("/main/java", "/main/resources");
			}


			List<String> refNames = getRefNames();
			List<String> refBeanNames = getBeanNames( refNames );
			StringBuffer fieldBuf = new StringBuffer();
			StringBuffer importPackBuf = new StringBuffer();
			StringBuffer getSetBuf = new StringBuffer();

			for (int i = 0; i < refBeanNames.size(); i++) {
					String beanName = refBeanNames.get(i);
					importPackBuf.append(" import ").append( modelPackagePath + "." + beanName
							+ ";\r\n\r\n");
					String lowerName = beanName.substring(0, 1).toLowerCase()
							+ beanName.substring(1, beanName.length());
					fieldBuf.append("    private " + beanName + " " + lowerName +" = null;\r\n");
//					fieldBuf.append("    private " + beanName + " " + lowerName +" = new  "+beanName+ "();\r\n");
					getSetBuf.append("    public void set").append(beanName)
							.append("( ").append(beanName).append(" ").append(lowerName)
							.append(" ){\r\n");
					getSetBuf.append("        this.").append(lowerName)
							.append("=").append(lowerName).append(";\r\n");
					getSetBuf.append("    }\r\n");
					getSetBuf.append("    public ").append( beanName).append(" get").append(beanName)
							.append("( ){\r\n");
					getSetBuf.append("    	return this.").append(lowerName)
							.append(";\r\n");
					getSetBuf.append("    }\r\n");
				 
			}
			FileReader read = new FileReader(filepath);
			BufferedReader br = new BufferedReader(read);
			boolean addImportFlag = true;
			boolean addFieldFlag = true;
			boolean addGetSetFlag = true;
			String row;
			while ((row = br.readLine()) != null) {
				filterRow(row,refBeanNames);
				if ((row.indexOf("class") != -1 && row.indexOf("public") != -1 && row
						.indexOf("{") != -1) && addImportFlag) {
					addImportFlag = false;
					content.append(importPackBuf.toString());
				}
				if ((row.indexOf("get") != -1 || row.indexOf("set") != -1)
						&& addFieldFlag) {
					addFieldFlag = false;
					content.append(fieldBuf.toString());
				}
				if ((row.indexOf("get") != -1 || row.indexOf("set") != -1)
						&& addGetSetFlag) {
					addGetSetFlag = false;
					content.append(getSetBuf.toString());
				}
				
				content.append(row).append("\r\n");
			}
			read.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content.toString();

	}

	private void filterRow(String row , List<String> refBeanNames){
		for( int i = 0 ; i < refBeanNames.size() ; i++ ) {
			String beanName = refBeanNames.get( i ) ; 
			String smallBeanName = beanName.substring(0, 1).toLowerCase()+beanName.substring(1, beanName.length());
			String indexStr = "private "+beanName+" "+smallBeanName+" = null;" ;
			if ( row.indexOf(indexStr) != -1 ){
				isSave = false ; 
				break;
			}
		}
	}
	public void setModelPackage(String modelPackagePath) {
		this.modelPackagePath = modelPackagePath ; 
	}

}
